package org.walklink.core.plugins.lb;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 默认负载均衡插件
 *
 * @author lidishan
 */
public class DefaultLoadBalancePlugin implements ILoadBalancePlugin {

    private static final AtomicInteger CURSOR = new AtomicInteger(0);

    @Override
    public String select() {
        // 1. TODO: 获取路由表所有信息（没有路由表信息，就尝试再次获取）
        List<String> routeInfos = new ArrayList<>();
        routeInfos.add("localhost:xxx");

        // 2. 轮询(溢出的时候会变为负数，这时候直接取绝对值)
        int index = Math.abs(CURSOR.getAndIncrement());
        return routeInfos.get(index % routeInfos.size());
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
